home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gscdevn.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  10.7 KB  |  352 lines

  1. /* Copyright (C) 1997, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gscdevn.c,v 1.5 2000/09/19 19:00:26 lpd Exp $ */
  20. /* DeviceN color space and operation definition */
  21. #include "memory_.h"
  22. #include "gx.h"
  23. #include "gserrors.h"
  24. #include "gscdevn.h"
  25. #include "gsfunc.h"
  26. #include "gsrefct.h"
  27. #include "gsmatrix.h"        /* for gscolor2.h */
  28. #include "gsstruct.h"
  29. #include "gxcspace.h"
  30. #include "gxcdevn.h"
  31.  
  32. /* ---------------- Color space ---------------- */
  33.  
  34. /* GC descriptors */
  35. gs_private_st_composite(st_color_space_DeviceN, gs_paint_color_space,
  36.      "gs_color_space_DeviceN", cs_DeviceN_enum_ptrs, cs_DeviceN_reloc_ptrs);
  37. private_st_device_n_map();
  38.  
  39. /* Define the DeviceN color space type. */
  40. private cs_proc_num_components(gx_num_components_DeviceN);
  41. private cs_proc_base_space(gx_alt_space_DeviceN);
  42. private cs_proc_equal(gx_equal_DeviceN);
  43. private cs_proc_init_color(gx_init_DeviceN);
  44. private cs_proc_restrict_color(gx_restrict_DeviceN);
  45. private cs_proc_concrete_space(gx_concrete_space_DeviceN);
  46. private cs_proc_concretize_color(gx_concretize_DeviceN);
  47. private cs_proc_remap_concrete_color(gx_remap_concrete_DeviceN);
  48. private cs_proc_install_cspace(gx_install_DeviceN);
  49. private cs_proc_adjust_cspace_count(gx_adjust_cspace_DeviceN);
  50. const gs_color_space_type gs_color_space_type_DeviceN = {
  51.     gs_color_space_index_DeviceN, true, false,
  52.     &st_color_space_DeviceN, gx_num_components_DeviceN,
  53.     gx_alt_space_DeviceN, gx_equal_DeviceN,
  54.     gx_init_DeviceN, gx_restrict_DeviceN,
  55.     gx_concrete_space_DeviceN,
  56.     gx_concretize_DeviceN, gx_remap_concrete_DeviceN,
  57.     gx_default_remap_color, gx_install_DeviceN,
  58.     gx_adjust_cspace_DeviceN, gx_no_adjust_color_count
  59. };
  60.  
  61. /* GC procedures */
  62.  
  63. private 
  64. ENUM_PTRS_WITH(cs_DeviceN_enum_ptrs, gs_color_space *pcs)
  65. {
  66.     return ENUM_USING(*pcs->params.device_n.alt_space.type->stype,
  67.               &pcs->params.device_n.alt_space,
  68.               sizeof(pcs->params.device_n.alt_space), index - 2);
  69. }
  70. ENUM_PTR(0, gs_color_space, params.device_n.names);
  71. ENUM_PTR(1, gs_color_space, params.device_n.map);
  72. ENUM_PTRS_END
  73. private RELOC_PTRS_WITH(cs_DeviceN_reloc_ptrs, gs_color_space *pcs)
  74. {
  75.     RELOC_PTR(gs_color_space, params.device_n.names);
  76.     RELOC_PTR(gs_color_space, params.device_n.map);
  77.     RELOC_USING(*pcs->params.device_n.alt_space.type->stype,
  78.         &pcs->params.device_n.alt_space,
  79.         sizeof(gs_base_color_space));
  80. }
  81. RELOC_PTRS_END
  82.  
  83. /* ------ Public procedures ------ */
  84.  
  85. /*
  86.  * Build a DeviceN color space.
  87.  */
  88. int
  89. gs_cspace_build_DeviceN(
  90.             gs_color_space **ppcspace,
  91.             gs_separation_name *psnames,
  92.             uint num_components,
  93.             const gs_color_space *palt_cspace,
  94.             gs_memory_t *pmem
  95.             )
  96. {
  97.     gs_color_space *pcspace = 0; /* bogus initialization */
  98.     gs_device_n_params *pcsdevn = 0; /* bogus initialization */
  99.     int code;
  100.  
  101.     if (palt_cspace == 0 || !palt_cspace->type->can_be_alt_space)
  102.     return_error(gs_error_rangecheck);
  103.  
  104.     code = gs_cspace_alloc(&pcspace, &gs_color_space_type_DeviceN, pmem);
  105.     if (code < 0)
  106.     return code;
  107.     pcsdevn = &pcspace->params.device_n;
  108.     code = alloc_device_n_map(&pcsdevn->map, pmem, "gs_cspace_build_DeviceN");
  109.     if (code < 0) {
  110.     gs_free_object(pmem, pcspace, "gs_cspace_build_DeviceN");
  111.     return code;
  112.     }
  113.     pcsdevn->names = psnames;
  114.     pcsdevn->num_components = num_components;
  115.     gs_cspace_init_from((gs_color_space *)&pcsdevn->alt_space, palt_cspace);
  116.     *ppcspace = pcspace;
  117.     return 0;
  118. }
  119.  
  120. /* Allocate and initialize a DeviceN map. */
  121. int
  122. alloc_device_n_map(gs_device_n_map ** ppmap, gs_memory_t * mem,
  123.            client_name_t cname)
  124. {
  125.     gs_device_n_map *pimap;
  126.  
  127.     rc_alloc_struct_1(pimap, gs_device_n_map, &st_device_n_map, mem,
  128.               return_error(gs_error_VMerror), cname);
  129.     pimap->tint_transform = 0;
  130.     pimap->tint_transform_data = 0;
  131.     pimap->cache_valid = false;
  132.     *ppmap = pimap;
  133.     return 0;
  134. }
  135.  
  136. /*
  137.  * Set the DeviceN tint transformation procedure.
  138.  */
  139. int
  140. gs_cspace_set_devn_proc(gs_color_space * pcspace,
  141.             int (*proc)(P5(const gs_device_n_params *,
  142.                        const float *,
  143.                        float *,
  144.                        const gs_imager_state *,
  145.                        void *
  146.                        )),
  147.             void *proc_data
  148.             )
  149. {
  150.     gs_device_n_map *pimap;
  151.  
  152.     if (gs_color_space_get_index(pcspace) != gs_color_space_index_DeviceN)
  153.     return_error(gs_error_rangecheck);
  154.     pimap = pcspace->params.device_n.map;
  155.     pimap->tint_transform = proc;
  156.     pimap->tint_transform_data = proc_data;
  157.     pimap->cache_valid = false;
  158.     return 0;
  159. }
  160.  
  161. /* Map a DeviceN color using a Function. */
  162. private int
  163. map_devn_using_function(const gs_device_n_params *pcsdevn,
  164.             const float *in, float *out,
  165.             const gs_imager_state *pis, void *data)
  166.  
  167. {
  168.     gs_function_t *const pfn = data;
  169.  
  170.     return gs_function_evaluate(pfn, in, out);
  171. }
  172.  
  173. /*
  174.  * Set the DeviceN tint transformation procedure to a Function.
  175.  */
  176. int
  177. gs_cspace_set_devn_function(gs_color_space *pcspace, gs_function_t *pfn)
  178. {
  179.     gs_device_n_map *pimap;
  180.  
  181.     if (gs_color_space_get_index(pcspace) != gs_color_space_index_DeviceN ||
  182.     pfn->params.m != pcspace->params.device_n.num_components ||
  183.     pfn->params.n !=
  184.       gs_color_space_num_components((gs_color_space *)
  185.                     &pcspace->params.device_n.alt_space)
  186.     )
  187.     return_error(gs_error_rangecheck);
  188.     pimap = pcspace->params.device_n.map;
  189.     pimap->tint_transform = map_devn_using_function;
  190.     pimap->tint_transform_data = pfn;
  191.     pimap->cache_valid = false;
  192.     return 0;
  193. }
  194.  
  195. /*
  196.  * If the DeviceN tint transformation procedure is a Function,
  197.  * return the function object, otherwise return 0.
  198.  */
  199. gs_function_t *
  200. gs_cspace_get_devn_function(const gs_color_space *pcspace)
  201. {
  202.     if (gs_color_space_get_index(pcspace) == gs_color_space_index_DeviceN &&
  203.     pcspace->params.device_n.map->tint_transform ==
  204.       map_devn_using_function)
  205.     return pcspace->params.device_n.map->tint_transform_data;
  206.     return 0;
  207. }
  208.  
  209. /* ------ Color space implementation ------ */
  210.  
  211. /* Return the number of components of a DeviceN space. */
  212. private int
  213. gx_num_components_DeviceN(const gs_color_space * pcs)
  214. {
  215.     return pcs->params.device_n.num_components;
  216. }
  217.  
  218. /* Return the alternate space of a DeviceN space. */
  219. private const gs_color_space *
  220. gx_alt_space_DeviceN(const gs_color_space * pcs)
  221. {
  222.     return (const gs_color_space *)&(pcs->params.device_n.alt_space);
  223. }
  224.  
  225. /* Test whether one DeviceN color space equals another. */
  226. private bool
  227. gx_equal_DeviceN(const gs_color_space *pcs1, const gs_color_space *pcs2)
  228. {
  229.     return (gs_color_space_equal(gx_alt_space_DeviceN(pcs1),
  230.                  gx_alt_space_DeviceN(pcs2)) &&
  231.         pcs1->params.device_n.num_components ==
  232.           pcs2->params.device_n.num_components &&
  233.         !memcmp(pcs1->params.device_n.names, pcs2->params.device_n.names,
  234.             pcs1->params.device_n.num_components *
  235.               sizeof(pcs1->params.device_n.names[0])) &&
  236.         pcs1->params.device_n.map->tint_transform ==
  237.           pcs2->params.device_n.map->tint_transform &&
  238.         pcs1->params.device_n.map->tint_transform_data ==
  239.           pcs2->params.device_n.map->tint_transform_data);
  240. }
  241.  
  242. /* Initialize a DeviceN color. */
  243. private void
  244. gx_init_DeviceN(gs_client_color * pcc, const gs_color_space * pcs)
  245. {
  246.     int i;
  247.  
  248.     for (i = 0; i < pcs->params.device_n.num_components; ++i)
  249.     pcc->paint.values[i] = 1.0;
  250. }
  251.  
  252. /* Force a DeviceN color into legal range. */
  253. private void
  254. gx_restrict_DeviceN(gs_client_color * pcc, const gs_color_space * pcs)
  255. {
  256.     int i;
  257.  
  258.     for (i = 0; i < pcs->params.device_n.num_components; ++i) {
  259.     floatp value = pcc->paint.values[i];
  260.  
  261.     pcc->paint.values[i] = (value <= 0 ? 0 : value >= 1 ? 1 : value);
  262.     }
  263. }
  264.  
  265. /* Remap a DeviceN color. */
  266. private const gs_color_space *
  267. gx_concrete_space_DeviceN(const gs_color_space * pcs,
  268.               const gs_imager_state * pis)
  269. {
  270.     /* We don't support concrete DeviceN spaces yet. */
  271.     const gs_color_space *pacs =
  272.     (const gs_color_space *)&pcs->params.device_n.alt_space;
  273.  
  274.     return cs_concrete_space(pacs, pis);
  275. }
  276.  
  277. private int
  278. gx_concretize_DeviceN(const gs_client_color * pc, const gs_color_space * pcs,
  279.               frac * pconc, const gs_imager_state * pis)
  280. {
  281.     int code, tcode;
  282.     gs_client_color cc;
  283.     const gs_color_space *pacs =
  284.     (const gs_color_space *)&pcs->params.device_n.alt_space;
  285.     gs_device_n_map *map = pcs->params.device_n.map;
  286.  
  287.     /* Check the 1-element cache first. */
  288.     if (map->cache_valid) {
  289.     int i;
  290.  
  291.     for (i = pcs->params.device_n.num_components; --i >= 0;) {
  292.         if (map->tint[i] != pc->paint.values[i])
  293.         break;
  294.     }
  295.     if (i < 0) {
  296.         int num_out = gs_color_space_num_components(pacs);
  297.  
  298.         for (i = 0; i < num_out; ++i)
  299.         pconc[i] = map->conc[i];
  300.         return 0;
  301.     }
  302.     }
  303.     /*
  304.      * We always map into the alternate color space.  We must preserve
  305.      * tcode for implementing a semi-hack in the interpreter.
  306.      */
  307.     tcode = (*pcs->params.device_n.map->tint_transform)
  308.     (&pcs->params.device_n, pc->paint.values, &cc.paint.values[0],
  309.      pis, pcs->params.device_n.map->tint_transform_data);
  310.     if (tcode < 0)
  311.     return tcode;
  312.     code = (*pacs->type->concretize_color) (&cc, pacs, pconc, pis);
  313.     return (code < 0 || tcode == 0 ? code : tcode);
  314. }
  315.  
  316. private int
  317. gx_remap_concrete_DeviceN(const frac * pconc,
  318.     gx_device_color * pdc, const gs_imager_state * pis, gx_device * dev,
  319.               gs_color_select_t select)
  320. {
  321.     /* We don't support concrete DeviceN colors yet. */
  322.     return_error(gs_error_rangecheck);
  323. }
  324.  
  325. /* Install a DeviceN color space. */
  326. private int
  327. gx_install_DeviceN(const gs_color_space * pcs, gs_state * pgs)
  328. {
  329.     /*
  330.      * Give an error if any of the separation names are duplicated.
  331.      * We can't check this any earlier.
  332.      */
  333.     const gs_separation_name *names = pcs->params.device_n.names;
  334.     uint i, j;
  335.  
  336.     for (i = 1; i < pcs->params.device_n.num_components; ++i)
  337.     for (j = 0; j < i; ++j)
  338.         if (names[i] == names[j])
  339.         return_error(gs_error_rangecheck);
  340.     return (*pcs->params.device_n.alt_space.type->install_cspace)
  341.     ((const gs_color_space *) & pcs->params.device_n.alt_space, pgs);
  342. }
  343.  
  344. /* Adjust the reference count of a DeviceN color space. */
  345. private void
  346. gx_adjust_cspace_DeviceN(const gs_color_space * pcs, int delta)
  347. {
  348.     rc_adjust_const(pcs->params.device_n.map, delta, "gx_adjust_DeviceN");
  349.     (*pcs->params.device_n.alt_space.type->adjust_cspace_count)
  350.     ((const gs_color_space *)&pcs->params.device_n.alt_space, delta);
  351. }
  352.